import {Button, Input, Tree, TreeProps} from 'antd';
import React, {Key} from 'react';
import {BasicDataNode, DataNode} from 'rc-tree/lib/interface';

import {ScanSearchTreeExtText} from "@/components/ant-ext/ScanSearchTreeExtText";
import {DropHandle} from "@/components/ant-ext/HSearchTreeExtDropHandle";
import {EventDataNode} from "antd/es/tree";
import {ReloadOutlined} from "@ant-design/icons";


export default function GetSelectNode(treeData: HSearchTreePops["treeData"], node: EventDataNode<any>) {
    const indexArray = node.pos.split('-');
    let selectNodes: HSearchTreePops["treeData"] = []
    for (let i = 1; i < indexArray.length; i++) {
        if (i === 1) {
            selectNodes.push(treeData[indexArray[i]])
        } else {
            selectNodes.push(selectNodes[selectNodes.length - 1].children![indexArray[i]])
        }
    }
    return selectNodes
}

export type HSearchTreePops<DataType extends BasicDataNode = DataNode> = {
    treeData: (DataType & { tag?: string, level?: number })[];
    onDrop?: (handler: DropHandle) => void;
    refreshExpandedKeys?: Key[]
    reload?: () => void;
    loading?: boolean;
} & TreeProps

export class HSearchTree extends React.Component<HSearchTreePops, any> {
    state: { treeDrop: TreeProps["onDrop"], expandedKeys: Key[], searchValue: string, autoExpandParent: boolean, treeData: HSearchTreePops["treeData"] } = {
        expandedKeys: [],
        searchValue: '',
        autoExpandParent: true,
        treeDrop: undefined,
        treeData: []
    };

    constructor(props: HSearchTreePops) {
        super(props);
        this.onSearchChange = this.onSearchChange.bind(this)
        this.onExpand = this.onExpand.bind(this)
        if (props.draggable && props.onDrop !== undefined) {
            this.state.treeDrop = (even) => {
                const {onDrop, treeData} = this.props
                const dropHandle = new DropHandle(even, treeData)
                if (dropHandle.hasMove) {
                    onDrop!(dropHandle)
                }
            }
        }
        this.state.treeData = ScanSearchTreeExtText("", [], undefined, props.treeData)

    }

    componentDidUpdate(prevProps: Readonly<HSearchTreePops>, prevState: Readonly<any>, snapshot?: any) {
        if (this.props.refreshExpandedKeys !== undefined && this.props.refreshExpandedKeys.length > 0 && this.props.refreshExpandedKeys !== prevProps.refreshExpandedKeys) {
            this.setState({expandedKeys: this.props.refreshExpandedKeys})
        }
        if (this.props.treeData !== prevProps.treeData) {
            this.setState({treeData: ScanSearchTreeExtText(this.state.searchValue, [...this.state.expandedKeys], undefined, this.props.treeData)})
        }
    }

    onExpand(expandedKeys: Key[]) {
        this.setState({
            expandedKeys: expandedKeys,
            autoExpandParent: false,
        });
    }

    onSearchChange(event: any) {
        const {value} = event.target;
        const expandedKeys: Key[] = []

        const treeData = ScanSearchTreeExtText(value, expandedKeys, undefined, this.props.treeData)
        this.setState({
            expandedKeys: expandedKeys.length > 0 ? expandedKeys : this.state.expandedKeys,
            searchValue: value,
            autoExpandParent: true,
            treeData: treeData
        });
    }


    render() {
        const {expandedKeys, autoExpandParent, treeDrop, treeData} = this.state;
        const {refreshExpandedKeys, reload, loading, ...other} = this.props;

        return (
            <div>
                <Input.Search style={{marginBottom: 8}} placeholder="请输入名称搜索" onChange={this.onSearchChange}
                              loading={loading} enterButton={<Button onClick={reload} icon={<ReloadOutlined/>}/>}/>
                <Tree {...other}
                      onExpand={this.onExpand}
                      treeData={treeData}
                      expandedKeys={expandedKeys}
                      autoExpandParent={autoExpandParent}
                      onDrop={treeDrop}
                />
            </div>
        );
    }
}
